class Solution
{
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target)
    {
        ranges::sort(candidates);
        int n = candidates.size();
        vector<vector<int>> ans;
        vector<int> path;
        function<void(int, int)> dfs = [&](int i, int sum) {
            if (sum == target)
            {
                ans.push_back(path);
                return;
            }
            for (int j = 0; j < n; j++)
            {
                if (sum < target && path.empty() || candidates[j] >= path.back())
                {
                    path.push_back(candidates[j]);
                    dfs(j + 1, sum + candidates[j]);
                    path.pop_back();
                }
            }
            };
        dfs(0, 0);
        return ans;
    }
};